﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Script.Serialization;
using VQWEB.common;
using VQWEB.huongnghiep.BL;

namespace VQWEB.huongnghiep.handlers
{
    /// <summary>
    /// Summary description for ProfessionHanler
    /// </summary>
    public class ProfessionHanler : IHttpHandler, IRouteHandler
    {
        ProfessionData ProfessionData = new ProfessionData();
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
            var url = context.Request.UrlReferrer.ToString();
            var json = string.Empty;
            var command = context.Request["cmd"];
            switch (command)
            {
                case "FillTraCuu":
                    var output = new
                        {
                            nhomnganh = ProfessionData.GetAllNhomNganh(),
                            nganh = ProfessionData.GetNganhByNhomId(-1),
                            hedaotao = ProfessionData.GetAllHeDaoTao(),
                            khoithi = ProfessionData.GetAllKhoiThi(),
                            tinhthanh = ProfessionData.GetAllTinhThanh()
                        };

                    json = serializer.Serialize(output);
                    break;
                case "ListNganhByNhom":
                    var nhomnganhid = context.Request["nhomnganhId"];
                    if (Ulties.IsNumeric(nhomnganhid))
                    {
                        json = serializer.Serialize(ProfessionData.GetNganhByNhomId(Convert.ToInt32(nhomnganhid)));
                    }
                    break;
                case "ListTruong":
                    var query = context.Request["query"];
                    var nhomnganh = Convert.ToInt32(context.Request["nhomnganh"]);
                    var nganh = Convert.ToInt32(context.Request["nganh"]);
                    var he = Convert.ToInt32(context.Request["he"]);
                    var khoi = Convert.ToInt32(context.Request["khoi"]);
                    var tinh = Convert.ToInt32(context.Request["tinh"]);
                    var diem = -1.0;
                    if(!string.IsNullOrEmpty(context.Request["diem"]))
                        diem = Convert.ToSingle(context.Request["diem"].Replace(",", "."));
                    var page =   Convert.ToInt32(context.Request["curPage"]);

                    json = serializer.Serialize(ProfessionData.SearchTruong(page, Const.university_PageSise, query, nhomnganh, nganh, he, khoi, tinh, diem));

                    break;
                case "viewTruong":
                    var truongId = Ulties.GetRequestId(url, "-", ".html");
                    json = serializer.Serialize(ProfessionData.ViewTruong(truongId));
                    break;
                case "compareTruong":
                    var t1 = context.Request["t1"];
                    var t2 = context.Request["t2"];
                    if (Ulties.IsNumeric(t1) && Ulties.IsNumeric(t2))
                    {
                        var dic = new Dictionary<string, object>
                                      {
                                          {"Truong1", ProfessionData.GetTruongDetail(Convert.ToInt32(t1))},
                                          {"Truong2", ProfessionData.GetTruongDetail(Convert.ToInt32(t2))}
                                      };

                        json = serializer.Serialize(dic);
                    }
                    break;
                case "viewDoanhNghiep":
                    var doanhnghiepId = Ulties.GetRequestId(url, "-", ".html");
                    json = serializer.Serialize(ProfessionData.ViewDoanhNghiep(doanhnghiepId));
                    break;
                case "ListNhomNganh_Nganh":
                    json = serializer.Serialize(ProfessionData.GetAllNhomNganhAndNganh());
                    break;
                case "ListChuyenNganh":
                    var nganhid = context.Request["nganhId"];
                    if (Ulties.IsNumeric(nganhid))
                    {
                        json = serializer.Serialize( ProfessionData.GetChuyenNganhByNganhId(Convert.ToInt32(nganhid)));
                    }
                    break;
                case "viewChuyenNganh":
                    var chuyennganhId = Ulties.GetRequestId(url, "-", ".html");
                    var objChuyenNganh = ProfessionData.GetChuyenNganhById(Convert.ToInt32(chuyennganhId));

                    var cndetail = new
                    {
                        Id = objChuyenNganh.chuyennganh_id,
                        Ten = objChuyenNganh.chuyennganh_ten,
                        Ma = objChuyenNganh.chuyennganh_ma,
                        NoiDung = objChuyenNganh.chuyennganh_gioithieu,
                        DaiHan = ProfessionData.GetTruongByChuyenNganhId(chuyennganhId,1, 5),
                        NganHan = ProfessionData.GetDoanhNghiepByChuyenNganhId(chuyennganhId,1, 5)

                    };

                    json = serializer.Serialize(cndetail);
                    break;

            }
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }

        public IHttpHandler GetHttpHandler(RequestContext RequestContext)
        {
            return this;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}